﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing;

namespace LaneLpnr
{
    public class CommonData
    {
       Common.ConfigHelper config = new Common.ConfigHelper();
        /// <summary>
        /// byte 转 二进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
                return returnStr;
            }
            else
            {
                return "0";
            }
        }


        public static void showpicture(int witdth, int height, int x, int y, PictureBox pic, byte[] buf)
        {
            pic.Width = witdth;
            pic.Height = height;
            pic.Location = new Point(x, y);
            try
            {
                //将byte数组 写入picture.image
                MemoryStream ms = new MemoryStream(); //新建内存流

                if (buf != null)
                {
                    ms.Write(buf, 0, buf.Length); //赋值
                    pic.Image = null;
                    pic.SizeMode = PictureBoxSizeMode.StretchImage;
                    pic.Image = Image.FromStream(ms); //读取流中内容
                    ms.Dispose();
                }
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Error("Exception showpicture " + ex.ToString());
            }
        }

        /// <summary>
        /// 保存抓拍图片
        /// </summary>
        /// <param name="buf"></param>
        public static string SaveTriggerPicture(string imagename, byte[] buf)
        {
            string imagepath = Common.ConfigHelper.GetConnectionStringConfig("ImagePath");//读取配置文件内容
            string childpath = DateTime.Now.ToString("yyyy-MM-dd");
            string path = System.IO.Path.Combine(imagepath, childpath);
            string imagepathname = System.IO.Path.Combine(path, imagename);
            // string imagepathname = path +"\\"+ imagename;

            try
            {
                if (!Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                FileInfo finfo = new FileInfo(path);
                if (finfo.Exists && finfo.Name != DateTime.Now.ToString("yyyy-MM-dd"))
                {
                    childpath = DateTime.Now.ToString("yyyy-MM-dd");
                    path = System.IO.Path.Combine(imagepath, childpath);
                    imagepathname = System.IO.Path.Combine(path, imagename);
                }
                System.IO.File.WriteAllBytes(imagepathname, buf);

                return imagepathname;
            }
            catch (Exception ex)
            {
                throw new Exception("保存图片失败:" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 保存抓拍图片
        /// </summary>
        /// <param name="buf"></param>
        public static void SaveTriggerPicture_old(string imagename, byte[] buf)
        {
            string imagepath = Common.ConfigHelper.GetConnectionStringConfig("ImagePath");//读取配置文件内容
            string imagepathname = System.IO.Path.Combine(imagepath, imagename);

            if (!Directory.Exists(imagepath))
            {
                System.IO.Directory.CreateDirectory(imagepath);
            }


            try
            {
                System.IO.File.WriteAllBytes(imagepathname, buf);
            }
            catch (Exception ex)
            {
                throw new Exception("保存图片失败:" + ex.ToString());
            }
        }

    }
}
